package com.start.workflow.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.common.details.BaseUserDetails;
import com.start.common.entity.StartBaseUser;
import com.start.common.mybaitsplus.StartMBColumnTools;
import com.start.common.tools.StartMapTools;
import com.start.common.tools.StartRedisTools;
import com.start.common.tools.StartStreamTools;
import com.start.common.tools.StartTools;
import com.start.common.user.StartUserInfoLoadService;
import com.start.security.StartTokenTools;
import com.start.workflow.act.StartActMyHandlerAuthTask;
import com.start.workflow.common.model.StartActBusinessModel;
import com.start.workflow.core.mappers.StartActBusinessMapper;
import com.start.workflow.core.service.StartActBusinessService;
import com.start.workflow.entity.StartActBusinessEntity;
import com.start.workflow.vo.StartActBusinessVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StartActBusinessServiceImpl extends ServiceImpl<StartActBusinessMapper, StartActBusinessEntity> implements StartActBusinessService {
    @Override
    public StartActBusinessEntity getOneByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<StartActBusinessEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StartActBusinessEntity::getProcInstId, processInstanceId);
        return getOne(lambdaQueryWrapper, false);
    }

    @Override
    public Page<StartActBusinessVo> pageBusiness(Page<StartActBusinessVo> page, Wrapper<StartActBusinessVo> wrapper) {
        Page<StartActBusinessVo> pageBusiness = baseMapper.pageBusiness(page, wrapper);
        if (pageBusiness.getRecords().isEmpty()) return pageBusiness;
        List<String> useKeyList = pageBusiness.getRecords().stream().map(StartActBusinessModel::getProcStartUserKey).distinct().collect(Collectors.toList());
        Map<String, Map<String, StartBaseUser>> usersMap = StartUserInfoLoadService.getInstance().getUsersMap(useKeyList);
        for (StartActBusinessVo actBusinessVo : pageBusiness.getRecords()) {
            if (!usersMap.containsKey(actBusinessVo.getProcStartUserType())) {
                continue;
            }
            Map<String, StartBaseUser> userMap = usersMap.get(actBusinessVo.getProcStartUserType());
            if (!userMap.containsKey(actBusinessVo.getProcStartUserId())) {
                continue;
            }
            StartBaseUser startBaseUser = userMap.get(actBusinessVo.getProcStartUserId());
            startBaseUser.setUserPwd(null);
            actBusinessVo.setProcStartUser(startBaseUser);
        }

        return pageBusiness;
    }

    @Override
    public Page<StartActMyHandlerAuthTask> pageMyNodeApply(Page<StartActMyHandlerAuthTask> page, Wrapper<StartActMyHandlerAuthTask> queryWrapper) {

        return null;
    }


    /**
     * 查询我的申请
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public Page<StartActBusinessEntity> pageMyBusiness(Page<StartActBusinessEntity> page, QueryWrapper<StartActBusinessEntity> wrapper) {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser();
        wrapper.eq(StartMBColumnTools.getColumn(StartActBusinessEntity::getProcStartUserKey), currentUser.getStartUserKey());
        return page(page, wrapper);
    }
}
